package my.sweep_tchap;
/**
 *
 * @author Tyler Chapman
 * @version 0.1
 * 
 * Class to connect with the database containing the street locations and
 * their times. It is called from StreetPage, StreetInput and AlarmPage. 
 * It is currently set to connect to my local page, but can be changed to 
 * connect to the correct site at a future date.
 * 
 */

import java.util.ArrayList;
import java.util.Map;
import org.xmlvm.iphone.NSData;
import org.xmlvm.iphone.NSDate;
import org.xmlvm.iphone.NSErrorHolder;
import org.xmlvm.iphone.NSHTTPURLResponseHolder;
import org.xmlvm.iphone.NSMutableURLRequest;
import org.xmlvm.iphone.NSURL;
import org.xmlvm.iphone.NSURLConnection;
import org.xmlvm.iphone.NSXMLParser;
import org.xmlvm.iphone.NSXMLParserDelegate;

public class connect{
	public static final int MINUTES_IN_FOURTY_YEARS = 21038400*60;
	public static final int MINUTES_IN_A_DAY = 1440*60;
	
	private ArrayList<String> streetList = new ArrayList<String>();
	private ArrayList<Integer> codeList = new ArrayList<Integer>();
	private String day;
	private String hour;
	private int streetCode;
    
	NSXMLParser xmlParser;
	
	public connect(){
		//localhost but can be changed to actual database
	   	NSURL url = NSURL.URLWithString("http://localhost:8888/streetCodesData.xml");
    	NSMutableURLRequest request = (NSMutableURLRequest)
    	NSMutableURLRequest.requestWithURL(url);
    	NSHTTPURLResponseHolder response = new NSHTTPURLResponseHolder();
    	NSErrorHolder error = new NSErrorHolder();
    	NSData data = NSURLConnection.sendSynchronousRequest(request, response, error);
    	NSData xml = NSData.dataWithBytes(data.getBytes());
    	xmlParser = new NSXMLParser(xml);
	}
    
    public void fillMap(double longitude, double latititude){
    	/*
    	 * Class to create the ArrayLists of the streets and the codes that correspond to these streets.
    	 * Finds the first four streets and returns them to be used by StreetPage.
    	 */
    	class MyParser extends NSXMLParserDelegate {
    		String startTag="";
    		String endTag="";
    		int i = 0;
    		Boolean found = false;
            
    		@Override
    		public void didStartElement(NSXMLParser parser, String elementName, String namespaceURI,
                                        String qualifiedName, Map<String, String> attributes) {
    			startTag = qualifiedName;
    		}
            
    		@Override
    		public void foundCharacters(NSXMLParser parser, String characters) {
    			if(startTag.equalsIgnoreCase("street") && !endTag.equalsIgnoreCase("street") && i<4){
    				streetList.add(characters);
    				System.out.println(streetList.get(i));
    				i++;
    				found = true;
    			}    	  		
    			if(found && startTag.equalsIgnoreCase("code") && !endTag.equalsIgnoreCase("code")){
    	  			System.out.println("day = " + characters);
    	  			codeList.add(Integer.parseInt(characters));
    	  			found = false;
    	  		}
                
    		}
            
    		@Override
    		public void didEndElement(NSXMLParser parser, String elementName, String namespaceURI,
                                      String qualifiedName) {
    			endTag = qualifiedName;
    		}
    	}
    	xmlParser.setDelegate(new MyParser());
    	xmlParser.parse();
    }
    
    public NSDate getDBSweepTime(final int streetCode){
    	/*
    	 * Class to find the sweep times according to the street code. Will return 
    	 * the correct NSDate object in current time zone.
    	 */
    	class MyParser extends NSXMLParserDelegate {
    		String startTag="";
    		String endTag="";
    		Boolean found = false;
    		@Override
    		public void didStartElement(NSXMLParser parser, String elementName, String namespaceURI,
                                        String qualifiedName, Map<String, String> attributes) {
    			startTag = qualifiedName;
    		}
    		@Override
    		public void foundCharacters(NSXMLParser parser, String characters) {
    	  		if(startTag.equalsIgnoreCase("code") && !endTag.equalsIgnoreCase("code") && characters.equalsIgnoreCase(Integer.toString(streetCode))){
    	  			found=true;
    	  		}
    	  		if(found && startTag.equalsIgnoreCase("day") && !endTag.equalsIgnoreCase("day")){
    	  			System.out.println("day = " + characters);
    	  			day = characters;
    	  		}
    	  		else if(found && startTag.equalsIgnoreCase("hour") && !endTag.equalsIgnoreCase("hour") && !endTag.equalsIgnoreCase("table")){
    	  			System.out.println("hour = " + characters);
    	  			hour = characters;
    	  			found = false;
    	  		}
    		}
    		@Override
    		public void didEndElement(NSXMLParser parser, String elementName, String namespaceURI,
                                      String qualifiedName) {
    			endTag = qualifiedName;
    		}
    	}
        
    	xmlParser.setDelegate(new MyParser());
    	xmlParser.parse();    
    	return NSDate.dateWithTimeIntervalSince1970(Integer.parseInt(day)*MINUTES_IN_A_DAY + 
                                                    MINUTES_IN_FOURTY_YEARS + Integer.parseInt(hour)*60*60);
	}
    
	public int searchStreetCode(final String streetName){
		/*
		 * Class to find the street code given a street name. This is used by StreetInput
		 *  to find the times of sweeping if they exist.
		 */
		class MyParser extends NSXMLParserDelegate {
    		String startTag="";
    		String endTag="";
    		Boolean found = false;
    		@Override
    		public void didStartElement(NSXMLParser parser, String elementName, String namespaceURI,
                                        String qualifiedName, Map<String, String> attributes) {
    			startTag = qualifiedName;
    		}
    		@Override
    		public void foundCharacters(NSXMLParser parser, String characters) {
    			if(startTag.equalsIgnoreCase("street") && !endTag.equalsIgnoreCase("street") && characters.equalsIgnoreCase(streetName)){
    	  			found=true;
    	  		}
    	  		if(found && startTag.equalsIgnoreCase("code") && !endTag.equalsIgnoreCase("code")){
    	  			streetCode = Integer.parseInt(characters);
    	  			found = false;
    	  		}
    		}
    		@Override
    		public void didEndElement(NSXMLParser parser, String elementName, String namespaceURI,
                                      String qualifiedName) {
    			endTag = qualifiedName;
    		}
    	}
        
    	xmlParser.setDelegate(new MyParser());
    	xmlParser.parse();
    	return streetCode;
	}
    
	public ArrayList<String> getStreetList() {
		return streetList;
	}
    
	public void setStreetList(ArrayList<String> streetList) {
		this.streetList = streetList;
	}
    
	public ArrayList<Integer> getCodeList() {
		return codeList;
	}
    
	public void setCodeList(ArrayList<Integer> codeList) {
		this.codeList = codeList;
	}
}

